<?php

class CategoryController extends AuthController{
	public $DB;
	public function __Init(){
		$this->DB = new CategoryModel;
	}

	public function index(){
		$data = $this->DB->getCategory();
		$count = $this->DB->count();
		$this->assign('count',$count);
		$this->assign('list',$data);
		$this->display('blogList.html');
	}
	public function edit(){
		$cid = $_GET['cid'];
		if(ISPOST){
			if($this->DB->editCategory($cid)){
				$this->success('修改成功',__MODEL__.'&c=Category&a=index');
			}else{
				$this->error($this->DB->error);
				return false;
			}
		}else{
			$data = $this->DB->where("cid = {$cid}")->find();
			$this->assign('data',$data);
			$this->display('edit.html');
		}

	}

	public function delete(){
		$cid = $_GET['cid'];
		if(empty($cid)){
			$this->error($this->DB->error,__MODEL__.'&c=Category&a=index');
			return false;
		}
		if($this->DB->deleteCategory($cid)){
			$this->success('删除成功',__CONTROLLER__.'&a=index');
		}else{
			$this->error($this->DB->error);
		}
	}

	public function addCatgory(){
		if(ISPOST){
			if($_POST['catname']!=''){
				if($this->DB->AddCat()){
					$this->success('添加分类成功',__CONTROLLER__);
					return true;
				}else{
					$this->error($this->DB->error,__CONTROLLER__);
					return false;
				}
			}else{
				$this->error('请填写分类名称',__CONTROLLER__.'&a=addCatgory');
				return false;
			}
		}else{
			$this->display('addCatgory.html');
		}
	}
}
?>